package com.base.lib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.base.lib.R;


/**
 * Date: 2019/10/15 14:26
 * Desc: 公共的可点击的item：类似个人中心单条带箭头item
 *
 * @author neo.duan
 */
public class ClickItemView extends FrameLayout {

    private static final String TAG_LINE = "tag_line";
    private static final InputFilter[] NO_FILTERS = new InputFilter[0];
    private boolean mTopLineEnable;
    private float mTopLineMarginStart;
    private float mTopLineMarginEnd;
    private boolean mBottomLineEnable;
    private float mBottomLineMarginStart;
    private float mBottomLineMarginEnd;
    private int mLeftIconId;
    private CharSequence mLeftText;
    private float mLeftTextSize;
    private boolean mIsMustInput;
    private int mRightIconId;
    private CharSequence mRightText;
    private float mRightTextSize;
    private boolean mInputEnable;
    private int mLeftTextColorId;
    private int mRightTextColorId;
    private boolean mRightIconEnable;
    private View mTopLine;
    private View mBottomLine;
    private ImageView mIvLeft;
    private TextView mTvLeft;
    private TextView mTvMust;
    private ImageView mIvRight;
    private EditText mEditRight;
    /**
     * 中间布局容器
     */
    private ViewGroup mContainer;
    private View mContentView;
    private CharSequence mRightHintText;
    private int mTopLineColorId;
    private int mBottomLineColorId;
    private float mLeftIconMarginEnd;
    private int mInputLength;
    private int mInputType;

    public ClickItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCustomAttr(context, attrs);
        initView();
    }

    public ClickItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClickItemView(Context context) {
        this(context, null);
    }

    private void initCustomAttr(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ClickItemView);
        // 获取自定义属性资源ID
        mTopLineEnable = a.getBoolean(R.styleable.ClickItemView_topLine, false);
        mTopLineMarginStart = a.getDimension(R.styleable.ClickItemView_topLineMarginStart, 0);
        mTopLineMarginEnd = a.getDimension(R.styleable.ClickItemView_topLineMarginEnd, 0);
        mTopLineColorId = a.getColor(R.styleable.ClickItemView_topLineColor, ContextCompat.getColor(context, R.color.line));
        mBottomLineEnable = a.getBoolean(R.styleable.ClickItemView_bottomLine, true);
        mBottomLineMarginStart = a.getDimension(R.styleable.ClickItemView_bottomLineMarginStart, dp2px(20));
        mBottomLineMarginEnd = a.getDimension(R.styleable.ClickItemView_bottomLineMarginEnd, dp2px(20));
        mBottomLineColorId = a.getColor(R.styleable.ClickItemView_bottomLineColor, ContextCompat.getColor(context, R.color.line));
        mLeftIconId = a.getResourceId(R.styleable.ClickItemView_leftIcon, -1);
        mLeftIconMarginEnd = a.getDimension(R.styleable.ClickItemView_leftIconMarginEnd, 0);
        mLeftText = a.getText(R.styleable.ClickItemView_leftText);
        mLeftTextSize = a.getDimension(R.styleable.ClickItemView_leftTextSize, -1);
        mIsMustInput = a.getBoolean(R.styleable.ClickItemView_isMustVisible, false);
        mRightIconId = a.getResourceId(R.styleable.ClickItemView_rightIcon, R.mipmap.ic_arrow_in);
        mRightText = a.getText(R.styleable.ClickItemView_rightText);
        mRightHintText = a.getText(R.styleable.ClickItemView_rightHintText);
        mRightTextSize = a.getDimension(R.styleable.ClickItemView_rightTextSize, -1);
        mInputEnable = a.getBoolean(R.styleable.ClickItemView_inputEnable, false);
        mLeftTextColorId = a.getColor(R.styleable.ClickItemView_leftTextColor, ContextCompat.getColor(context, R.color.textBlack));
        mRightTextColorId = a.getColor(R.styleable.ClickItemView_rightTextColor, ContextCompat.getColor(context, R.color.textBlack));
        mRightIconEnable = a.getBoolean(R.styleable.ClickItemView_rightIconVisible, true);
        mInputLength = a.getInt(R.styleable.ClickItemView_inputLength, -1);
        mInputType = a.getInt(R.styleable.ClickItemView_inputType, 0);

        a.recycle();
    }

    /**
     * 初始化布局信息
     */
    private void initView() {
        //添加顶部线
        addView(mTopLine = createLine(Gravity.TOP, (int) mTopLineMarginStart, (int) mTopLineMarginEnd, mTopLineColorId));
        //添加中间内容
        View.inflate(getContext(), R.layout.layout_click_item_view, this);
        //添加底部线
        addView(mBottomLine = createLine(Gravity.BOTTOM, (int) mBottomLineMarginStart, (int) mBottomLineMarginEnd, mBottomLineColorId));

        setBackgroundResource(android.R.color.white);

        mIvLeft = findViewById(R.id.iv_item_view_left);
        mTvLeft = findViewById(R.id.tv_item_view_left);
        mTvMust = findViewById(R.id.tv_item_view_must);
        mIvRight = findViewById(R.id.iv_item_view_right);
        mEditRight = findViewById(R.id.edt_item_view_right);
        mContainer = findViewById(R.id.rl_item_view_container);

        setInputEnable(mInputEnable);
        enableTopLine(mTopLineEnable);
        enableBottomLine(mBottomLineEnable);
        setLeftIcon(mLeftIconId);
        setLeftIconMargin((int) mLeftIconMarginEnd);
        setLeftText(String.valueOf(mLeftText));
        setLeftTextSize(mLeftTextSize);
        setIsMustInput(mIsMustInput);
        setRightIcon(mRightIconId);
        setRightText(String.valueOf(mRightText));
        setRightHintText(String.valueOf(mRightHintText));
        setRightTextSize(mRightTextSize);
        setLeftTextColor(mLeftTextColorId);
        setRightTextColor(mRightTextColorId);
        enableRightIcon(mRightIconEnable);
        setInputLength(mInputLength);
        setInputType(mInputType);
        mEditRight.setMaxLines(1);
    }


    private void setInputType(int inputType) {
        // text 0 phone 1 number 2 money 3
        switch (inputType) {
            case 1:
                mEditRight.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
            case 2:
                mEditRight.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 3:
                mEditRight.setInputType(InputType.TYPE_CLASS_PHONE);
                mEditRight.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(2), new InputFilter.LengthFilter(8)});
                break;
            default:
                break;
        }
    }

    private void setInputLength(int length) {
        if (length >= 0) {
            mEditRight.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        } else {
            mEditRight.setFilters(NO_FILTERS);
        }
    }

    private void setLeftIconMargin(int margin) {
        if (margin > 0) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mIvLeft.getLayoutParams();
            layoutParams.rightMargin = margin;
            mIvLeft.setLayoutParams(layoutParams);
        }
    }

    /**
     * 创建线条
     *
     * @return line
     */
    private View createLine(int gravity, int marginStart, int marginEnd, int color) {
        View lineView = new View(getContext());
        lineView.setTag(TAG_LINE);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(0.5f), gravity);
        layoutParams.leftMargin = marginStart;
        layoutParams.rightMargin = marginEnd;
        lineView.setLayoutParams(layoutParams);
        lineView.setBackgroundColor(color);
        return lineView;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        if (childCount <= 0) {
            return;
        }
        View lastChildView = getChildAt(childCount - 1);

        if (!TAG_LINE.equals(lastChildView.getTag())) {
            this.removeView(lastChildView);
            setContentView(lastChildView);
        }
    }

    /**
     * 设置右边文本的颜色
     *
     * @param color color
     */
    private void setRightTextColor(int color) {
        mEditRight.setTextColor(color);
    }

    /**
     * 设置左边文本的颜色
     *
     * @param color color
     */
    private void setLeftTextColor(int color) {
        mTvLeft.setTextColor(color);
    }

    /**
     * 左边icon是否可用
     *
     * @param enable enable
     * @param resId  resId
     */
    public void enableLeftIcon(boolean enable, int resId) {
        if (enable) {
            mIvLeft.setImageResource(resId);
            mIvLeft.setScaleType(ScaleType.FIT_CENTER);
        }
        mIvLeft.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    public void enableLeftIcon(boolean enable) {
        enableLeftIcon(enable, R.mipmap.ic_launcher);
    }

    /**
     * 设置左边图片
     *
     * @param resId resId
     */
    public void setLeftIcon(int resId) {
        if (resId < 0) {
            enableLeftIcon(false);
            return;
        }
        enableLeftIcon(true, resId);
    }

    public void setLeftTextSize(float size) {
        if (size < 0) {
            return;
        }
        mTvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public String getLeftText() {
        return mTvLeft.getText().toString().trim();
    }

    /**
     * 设置左边文本
     *
     * @param text text
     */
    public void setLeftText(String text) {
        if (TextUtils.isEmpty(text) || "null".equals(text)) {
            text = "";
        }
        mTvLeft.setText(text);
    }

    public void setLeftText(int resId) {
        if (resId < 0) {
            mTvLeft.setText("");
            return;
        }
        mTvLeft.setText(getResources().getString(resId));
    }

    public void setTvLeftOnClick(OnClickListener listener) {
        mTvLeft.setOnClickListener(listener);
        mIvLeft.setOnClickListener(listener);
    }

    public void setIsMustInput(boolean isMustInput) {
        this.mIsMustInput = isMustInput;
        mTvMust.setVisibility(isMustInput ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 设置顶部线条是否可用
     *
     * @param enable enable
     */
    public void enableTopLine(boolean enable) {
        mTopLine.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置底部线条是否可用
     *
     * @param enable enable
     */
    public void enableBottomLine(boolean enable) {
        mBottomLine.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    /**
     * 右边icon是否可用
     *
     * @param enable enable
     * @param resId  resId
     */
    public void enableRightIcon(boolean enable, int resId) {
        if (enable) {
            if (resId > 0) {
                mIvRight.setImageResource(resId);
            }
        }
        mIvRight.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    public void enableRightIcon(boolean enable) {
        enableRightIcon(enable, R.mipmap.ic_arrow_in);
        if (enable) {
            setBackgroundResource(R.drawable.selector_white_0_round);
        } else {
            setBackgroundResource(R.color.white);
        }
    }

    /**
     * 设置右边图标
     *
     * @param resId resId
     */
    public void setRightIcon(int resId) {
        if (resId < 0) {
            enableRightIcon(false);
            return;
        }
        mIvRight.setImageResource(resId);
    }

    public String getRightText() {
        return mEditRight.getText().toString().trim();
    }

    /**
     * 设置右边文本
     *
     * @param resId resId
     */
    public void setRightText(int resId) {
        if (resId < 0) {
            mEditRight.setText("");
            return;
        }
        mEditRight.setText(getResources().getString(resId));
    }

    /**
     * 设置右边文本
     */
    public void setRightText(String text) {
        if (TextUtils.isEmpty(text) || "null".equals(text)) {
            text = "";
        }
        mEditRight.setText(text);
    }

    public void setRightTextSize(float size) {
        if (size < 0) {
            return;
        }
        mEditRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setContainerHide(boolean hide) {
        mContainer.setVisibility(hide ? GONE : VISIBLE);
    }

    public EditText getEditText() {
        return mEditRight;
    }

    public boolean getInputEnable() {
        return mInputEnable;
    }

    /**
     * 右边输入是否可用
     */
    public void setInputEnable(boolean enable) {
        mEditRight.setFocusable(enable);
        mEditRight.setFocusableInTouchMode(enable);
        mEditRight.setCursorVisible(enable);
        mEditRight.setClickable(enable);
        mEditRight.setEnabled(enable);
    }

    /**
     * 设置右边hint文本
     */
    public void setRightHintText(String text) {
        if (TextUtils.isEmpty(text) || "null".equals(text)) {
            text = "";
        }
        mEditRight.setHint(text);
    }

    /**
     * 获取中间容器填充的View
     *
     * @return contentView
     */
    public View getContentView() {
        return mContentView;
    }

    /**
     * 设置中间容器view
     *
     * @param layoutResID 资源id
     */
    public void setContentView(int layoutResID) {
        setContentView(View.inflate(getContext(), layoutResID, null));
    }

    /**
     * 设置中间容器view
     *
     * @param contentView view
     */
    public void setContentView(View contentView) {
        this.mContentView = contentView;
        mContainer.removeAllViews();
        mContainer.addView(contentView);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        getEditText().setFocusable(enabled);
        getEditText().setFocusableInTouchMode(enabled);
        getEditText().setCursorVisible(enabled);
    }

    private int dp2px(float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
    }
}
